﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using S9Engine;

public partial class Default : Parser
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            _parser.SetCurrentDir("~/tpl");
            _parser.SetLanguageFile("~/templates/language.txt", _currentLanguage);
           
            Session["CurrentPage"] = "Default.aspx";

            LoadFile("home.html", false);
           
            LoadHomeProduct("Main", "AlcoholHomeProduct", "Alcohol");
            LoadHomeProduct("Main", "BeerHomeProduct", "Beer");

            _parser.Assign("language" + Session["Language"].ToString().ToLower(), "class=\"current\"");
        }
        catch (Exception ex)
        {
            WriteError(ex);
        }
        finally
        {
            _parser.Parse("Main");
            _parser.Out("Main");
        }
    }

    private void LoadHomeProduct(string containBlock, string parentBlock, string type)
    {

        var categories = LoadAllProduct("Code", type == "Alcohol" ? true : false, type == "Beer" ? true : false, 6).ToList();

        foreach (var c in categories)
        {
            _parser.Assign("productID", c.ID.ToString());
            _parser.Assign("categoryID", c.Parent.ToString());
            _parser.Assign("productName", c.Name);
            _parser.Assign("productImage", c.Image);
            _parser.Assign("productExcerpt", c.Excerpt);           

            _parser.Parse(string.Format("{0}.{1}.{2}Item", containBlock, parentBlock, parentBlock));
        }

        if (null != categories && 0 != categories.Count()) _parser.Parse(string.Format("{0}.{1}", containBlock, parentBlock));
    }
}